﻿using System.Xml.Linq;

namespace System.Xml.Serialization
{
    public static class XmlSerializationExtensions
    {
        /// <summary>
        /// Serializes an object to a System.Xml.Linq.XElement.
        /// </summary>
        /// <param name="xs">The System.Xml.Serialization.XmlSerializer used to generate the XML.</param>
        /// <param name="obj">The object being converted to XML.</param>
        /// <returns>A System.Xml.Linq.XElement representation of the object.</returns>
        public static XElement SerializeAsXElement(this XmlSerializer xs, object obj)
        {
            XmlSerializerNamespaces xsn = new XmlSerializerNamespaces();
            xsn.Add(string.Empty, string.Empty);

            XDocument d = new XDocument();
            using (XmlWriter w = d.CreateWriter()) xs.Serialize(w, obj, xsn);
            XElement e = d.Root;
            e.Remove();
            return e;
        }
    }
}